package com.zheng.russiaRec.view 
{
	import com.zheng.russiaRec.model.P_App;
	import com.zheng.russiaRec.model.vo.VO_App;
	import com.zheng.russiaRec.staticData.EventData;
	import com.zheng.utils.DebugTrace;
	
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * @describe  	键盘输入Mediator
	 * @author  	Mr.zheng
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class M_GameingKeyboard extends Mediator implements IMediator
	{
		public static const NAME:String="M_NAME_KEYBOARD";
		
		public function M_GameingKeyboard(viewComponent:Object = null) 
		{
			super(NAME, viewComponent);
			
			addKeyDown();
		}
		override public function listNotificationInterests():Array 
		{
			return [EventData.C_GAMEOVER, EventData.C_DEL_LINE, EventData.M_DELLINE_AM_OVER];
		}
		override public function handleNotification(notification:INotification):void 
		{
			switch (notification.getName()) 
			{
				case EventData.C_DEL_LINE:
					delKeyDown();
					break;
				case EventData.M_DELLINE_AM_OVER:
					addKeyDown();
					break;
				case EventData.C_GAMEOVER:
					delKeyDown();
					break;
			}
		}
		/** 
		 * 键盘按下
		 * 
		 * 原理描述：
		 * 多个方块组成的一个图形，这里我称为一个积木
		 * 积木从一个地方移动到另外一个地方，或者从一种形态转变到另外一种形态
		 * 其实是将积木从原来的显示区域清除，然后在已经计算好显示区域呈现
		 * 那么，我需要两个数组，
		 * 其一。组成积木的方块原始位置
		 * 其二。组成积木的方块新的位置
		 * 那么，我要完成两件事情
		 * 将第一个数组中各元素值改变为不显示
		 * 将第二个数组中个元素的值改为显示
		 * 
		 * @param	e
		 */ 
		private function onKeyDown(e:KeyboardEvent):void 
		{
			var type:int = e.keyCode;
			var obj:Object = new Object();
			var p_app:P_App=facade.retrieveProxy(P_App.NAME) as P_App;
			
			if(p_app.currGameStage!=VO_App.STAGE_GAMEING)return;
			
			p_app.userOP=type;
			if ((type == Keyboard.UP || type == Keyboard.DOWN || type == Keyboard.LEFT || 
			type == Keyboard.RIGHT)
			&&!p_app.isGamePause) {
				DebugTrace.monsterDebug(this,"按下键："+type);
				obj.userOP = type;
				if(type==Keyboard.UP){
					sendNotification(EventData.MC_BRICK_ROTAION,obj);
				}else{
					sendNotification(EventData.MC_BRICK_MOVE,obj);
				}
			}
			//狠狠落下
			if (type == Keyboard.SPACE) {
				sendNotification(EventData.MC_DROP_STRONG);
			}
			//游戏暂停
			if (type == Keyboard.NUMPAD_0) {
				if(p_app.isGamePause){
					sendNotification(EventData.M_GAME_RESUME);
				}else {
					sendNotification(EventData.M_GAME_PAUSE);
				}
				p_app.isGamePause = !p_app.isGamePause;
			}
		}
		private function addKeyDown():void 
		{
			Stage(viewComponent.appRoot).addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 
		}
		private function delKeyDown():void {
			Stage(viewComponent.appRoot).removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
	}
	
}